# -*- coding = utf-8 -*-
# @Time : 2023/6/6 11:15
# @Author :Henry L
# @File : GZ.py
# @Software : PyCharm
'http://61.243.4.129:8001/Home/List?ListType=list&name=%E9%80%9A%E7%9F%A5%E5%85%AC%E5%91%8A&Type=6BBB08AA-8E77-4E1B-8374-11151FAB926D&SysID=0'
'http://61.243.4.129:8001/Home/List?ListType=list&name=%E9%80%9A%E7%9F%A5%E5%85%AC%E5%91%8A&Type=6BBB08AA-8E77-4E1B-8374-11151FAB926D&SysID=1'
import glob
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


class SD:
    def __init__(self):
        options = webdriver.ChromeOptions()
        self.host = "https://ybj.fujian.gov.cn/"
        prefs = {"plugins.always_open_pdf_externally": True,
                 "download.default_directory": os.getcwd() + "\\"}
        options.add_experimental_option("prefs", prefs)
        self.driver = webdriver.Chrome(options=options)
        self.driver.maximize_window()
        self.wait = WebDriverWait(driver=self.driver, timeout=10)

    def get(self, page):
        for i in range(0,3):
            url = "http://61.243.4.129:8001/Home/List?ListType=list&name=%E9%80%9A%E7%9F%A5%E5%85%AC%E5%91%8A&" \
                  "Type=6BBB08AA-8E77-4E1B-8374-11151FAB926D&SysID={}".format(str(i))
            self.driver.get(url)
            time.sleep(3)
            # tbody = self.driver.find_element(By.XPATH, "/html/body/div[1]/div[3]/div/div/div[2]/div[4]/div")
            lan = self.driver.find_elements(By.XPATH, "/html/body/div[2]/div[1]/div/ul/li/a")
            date_list = self.driver.find_elements(By.XPATH, "/html/body/div[2]/div[1]/div/ul/li/a/span")
            for item in range(0, len(lan)):
                time.sleep(3)
                # if item.find_element(By.XPATH, "div[1]/div[2]").text == '公告标题':
                #     continue
                date = date_list[item].text
                lan[item].click()
                # handles = self.driver.window_handles
                # self.driver.switch_to.window(handles[-1])
                time.sleep(1.5)
                title = self.driver.find_element(By.ID, "title").get_attribute("innerText")
                box = self.driver.find_element(By.ID, "con")
                view_time = ''
                content = box.get_attribute("outerHTML")
                # attachments_1 = box.find_elements(By.TAG_NAME, "a")
                # attachments_2 = self.driver.find_elements(By.XPATH, "/html/body/div[1]/div[3]/div/div/div[2]/div[5]/div[2]/ul/li/a")
                attachments = box.find_elements(By.TAG_NAME, "a")
                attachments_list = []
                if len(attachments) > 0:
                    for a in attachments:
                        if a.text == "" or a.get_attribute("href") is None \
                                or "贵州省非免疫规划疫苗集中采购中标目录》并执行。" == a.get_attribute("innerText"):
                            continue
                        if "upload" in a.get_attribute("href") or "附件" in a.text:
                            self.driver.execute_script("window.scroll(0,500)")
                            a.click()
                            # attachments_list.append(a.get_attribute("href").replace('./', ''))
                            time.sleep(1)
                            # href = a.get_attribute("href").split('.')[0]
                time.sleep(1)
                while self.driver.current_url != url:
                    # self.driver.close()
                    # handles = self.driver.window_handles
                    # self.driver.switch_to.window(handles[-1])
                    self.driver.back()
                    time.sleep(1)
                data_list = [title, date, view_time, content, attachments_list]
                print(data_list)

    def get_last_filename_and_rename(self, sav_folder, new_filename):
        files = glob.glob(sav_folder + '/*')
        max_file = max(files, key=os.path.getctime)
        filename = max_file.split("/")[-1].split(".")[0]
        new_path = max_file.replace(filename, new_filename)
        os.rename(max_file, new_path)
        return new_path

    def close(self):
        time.sleep(60)
        self.driver.quit()

sd = SD()
sd.get("")
sd.close()

